package com.yanyeori.rabbitmqprovider.controller;

import com.yanyeori.framework.core.constant.BaseConstant;
import com.yanyeori.framework.core.model.WebResponse;
import com.yanyeori.framework.core.util.DateUtil;
import com.yanyeori.framework.core.util.IdUtil;
import com.yanyeori.rabbitmqprovider.constant.RabbitmqConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author chenkuan
 * @version v1.0
 * @desc 消息发送接口
 * @date 20/7/29 029 16:29
 */
@RestController
@RequestMapping("/sendMqMsgController")
public class SendMqMsgController {
	private static final Logger logger = LoggerFactory.getLogger(SendMqMsgController.class);

	@Autowired
	private RabbitTemplate rabbitTemplate;

	@RequestMapping("/sendDirectMsg")
	public WebResponse<String> sendDirectMsg(@RequestParam String messageData) {
		String messageId = IdUtil.randomUUID();
		String createTime = DateUtil.format(new Date(), BaseConstant.FORMAT_DATETIME);
		Map<String, Object> map = new HashMap<>();
		map.put("messageId", messageId);
		map.put("messageData", messageData);
		map.put("createTime", createTime);
		//将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
		rabbitTemplate.convertAndSend(RabbitmqConfig.DIRECT_EXCHANGE, RabbitmqConfig.DIRECT_ROUTING_KEY, map);
		logger.info("消息=[{}]发送成功", messageData);
		return new WebResponse<String>().message("OK");
	}

	@RequestMapping("/sendTopicMsg1")
	public WebResponse<String> sendTopicMsg1(@RequestParam String messageData) {
		String messageId = IdUtil.randomUUID();
		String createTime = DateUtil.format(new Date(), BaseConstant.FORMAT_DATETIME);
		Map<String, Object> map = new HashMap<>();
		map.put("messageId", messageId);
		map.put("messageData", messageData);
		map.put("createTime", createTime);
		//将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
		rabbitTemplate.convertAndSend(RabbitmqConfig.TOPIC_EXCHANGE, RabbitmqConfig.TOPIC_MAN, map);
		logger.info("消息=[{}]发送成功", messageData);
		return new WebResponse<String>().message("OK");
	}

	@RequestMapping("/sendTopicMsg2")
	public WebResponse<String> sendTopicMsg2(@RequestParam String messageData) {
		String messageId = IdUtil.randomUUID();
		String createTime = DateUtil.format(new Date(), BaseConstant.FORMAT_DATETIME);
		Map<String, Object> map = new HashMap<>();
		map.put("messageId", messageId);
		map.put("messageData", messageData);
		map.put("createTime", createTime);
		//将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
		rabbitTemplate.convertAndSend(RabbitmqConfig.TOPIC_EXCHANGE, RabbitmqConfig.TOPIC_WOMAN, map);
		logger.info("消息=[{}]发送成功", messageData);
		return new WebResponse<String>().message("OK");
	}

	@RequestMapping("/sendFanoutMsg")
	public WebResponse<String> sendFanoutMsg(@RequestParam String messageData) {
		String messageId = IdUtil.randomUUID();
		String createTime = DateUtil.format(new Date(), BaseConstant.FORMAT_DATETIME);
		Map<String, Object> map = new HashMap<>();
		map.put("messageId", messageId);
		map.put("messageData", messageData);
		map.put("createTime", createTime);
		//将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
		rabbitTemplate.convertAndSend(RabbitmqConfig.FANOUT_EXCHANGE, null, map);
		logger.info("消息=[{}]发送成功", messageData);
		return new WebResponse<String>().message("OK");
	}
}
